<?php

/*
 * This class is used for as Model for Product table 
 */

class Model_Zone extends Model_Base {

    protected $_name = 'zone';
    protected $_primaryKey = 'zone_id';

    public function getStatusArray() {
        return array(
            "" => "-- Select Status --",
            "1" => "Active",
            "0" => "Inactive"
        );
    }
    
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array("zone" => "zone"))
                ->where("zone_id = ?", $id)
                ->where("zone_status!= 2");
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    
    /**
     *  This function is used to get Questions
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchZone($where = "", $limit = "", $offset = "0", $order = 'zone_id', $sort = 'ASC') {
        $finalArray  = array();
        $select = $this->select()
                ->from(array('zone' => 'zone'))
                ->setIntegrityCheck(false)
                //->join(array('ln' => 'languages'), "zone.zone_lang_id = ln.lang_id",array('lang_name'))
                ->join(array('city' => 'city'), "zone.zone_city_id = city.city_id",array('city_name'))
                ->where("zone_status!= 2");
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        #echo $select;exit;
        $select->order($order . " " . $sort);
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            //$finalArray = array('' => '-- Select Zone --');
            //foreach ($data->toArray() as $_area_type) {
            //    $finalArray[$_area_type['zone_id']] = $_area_type['zone_zone'];
            //}
            return $data->toArray();
        } else {
            return array();
        }
    }
    
    public function fetchAllZone($where = "", $limit = "", $offset = "0", $order = 'zone_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('zone' => 'zone'))
                 ->setIntegrityCheck(false)
                //->join(array('ln' => 'languages'), "zone.zone_lang_id = ln.lang_id",array('lang_name'))
                ->join(array('city' => 'city'), "zone.zone_city_id = city.city_id",array('city_name'))
                ->where("zone_status!= 2");
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        $select->order($order . " " . $sort);

        $data = parent::fetchAll($select);
        if (!empty($data)) {
            
            foreach ($data->toArray() as $_area_type) {
                $finalArray[$_area_type['zone_id']] = $_area_type['zone_zone'];
            }
            return $finalArray;
        } else {
            return array();
        }
    }
    
    /**
     *  This function is used to get total orders
     * @return int
     */
    public function getZoneCount($where) {
        $select = $this->select()
                ->from(array("zone" => "zone"), Array("cnt" => "count(*)"))
                ->setIntegrityCheck(false)
                //->join(array('ln' => 'languages'), "zone.zone_lang_id = ln.lang_id",array('lang_name'))
                ->join(array('city' => 'city'), "zone.zone_city_id = city.city_id",array('city_name'))
                ->where("zone_status != 2");
        if ($where != "") {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }
    /**
     *  This function is used to delete record
     * 
     */
    public function deleteZone($zoneArray) {
        if (is_array($zoneArray)) {
            foreach ($zoneArray as $_zone) {
                $zoneData = Array();
                $zoneData = $this->fetchEntryById($_zone);
                if (count($zoneData) > 0) {
                    $data = array("zone_status" => "2");
                    $this->update($data, "zone_id =" . (int) $_zone);
                }
            }
        } else {
            $zone_id = $zoneArray;
            $zoneData = $this->fetchEntryById($zone_id);
            if (count($zoneData) > 0) {
                $data = array("zone_status" => "2");
                $this->update($data, "zone_id =" . $zone_id);
            }
        }
    }

}
